package Project;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Enumeration;

import com.oreilly.servlet.MultipartRequest;
import com.oreilly.servlet.multipart.DefaultFileRenamePolicy;

import bean.MyOracle;

public class BoardDTO {
	private BoardVO vo;
	private BoardVO mvo;
	private Connection conn;
	
	//페이지와 관련
	int listSize=5;  //페이지당 표시 되는 행수
	int blockSize=5; //한블럭당 표시되는 페이지 수
	int totSize=0;   //전체 게시물의 갯수
	int totPage=0;   //전체 페이지의 갯수
	int totBlock=0;  //전체 블럭수
	int nowPage=1;   //현재 페이지
	int nowBlock=1;  //현재 블럭
	int startNo=0;   //리스트 목록의 시작 위치
	int endNo=0;     //리스트 목록의 끝 위치
	int startPage=0; //현재블럭에서 표시되는 시작 블럭번호
	int endPage=0;   //현재블럭에서 표시되는 끝 블럭 번호
	
	public BoardDTO() {
		conn=new MyOracle().getConn();
	}

	public boolean insertuser()//실제 DB에 입력하게 위함..
	{
		boolean f = false;//f는 input_result에서 사용됨 오류표한을 위함
		String sql="insert into board3(mid, title, udate, content, pwd, rno) values(?,?,sysdate,?,?,SEQ_BOARD3_RNO.nextval)";
		try{
		PreparedStatement pst = conn.prepareStatement(sql);
		pst.setString(1, vo.getMid());
		pst.setString(2, vo.getTitle());
		pst.setString(3, vo.getContent());
		pst.setString(4, vo.getPwd());
		int cnt = pst.executeUpdate();
		System.out.println(vo.getMid());
		if(cnt>0)
		{
			f=true;
		}
		pst.close();
		conn.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		return f;
	}
	public boolean insertmaster()//실제 DB에 입력하게 위함..
	{
		boolean f = false;//f는 input_result에서 사용됨 오류표한을 위함
		String sql="insert into board3(masterid, mastertitle, masterdate, mastercontent, masterrno) values(?, ?,sysdate, ?,SEQ_BOARD3_MASTERRNO.nextval)";
		try{
		PreparedStatement pst = conn.prepareStatement(sql);
		pst.setString(1, vo.getMasterid());
		pst.setString(2, vo.getMastertitle());
		pst.setString(3, vo.getMastercontent());
		System.out.println(vo.getMasterid());
		System.out.println(vo.getMastertitle());
		System.out.println(vo.getMastercontent());
		
		int cnt = pst.executeUpdate();

		if(cnt>0)
		{
			f=true;
		}
		pst.close();
		conn.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		return f;
	}
	public BoardVO getVo() {
		return vo;
		
	}

	public void setVo(BoardVO vo) {
		
		this.vo = vo;
		this.nowPage=vo.getNowPage();
		this.nowBlock=vo.getNowBlock();
		int size=1024*1024*10;
		String saveDir="C:/Users/jhta/Documents/javaworkspace/webHTA/WebContent/3Photo/";
		String oriFile="";
		String sysFile="";
		if(vo.getRequest()!=null){
			MultipartRequest multi=null;
			try {
				
				multi=new MultipartRequest(vo.getRequest(), saveDir, size, "utf-8", new DefaultFileRenamePolicy());
				Enumeration files =multi.getFileNames();
				String tempFile=(String)files.nextElement();
				oriFile=multi.getOriginalFileName(tempFile);
				sysFile=multi.getFilesystemName(tempFile);
							
				this.vo.setMid(multi.getParameter("mid"));
				this.vo.setTitle(multi.getParameter("title"));
				this.vo.setPwd(multi.getParameter("pwd"));
				this.vo.setContent(multi.getParameter("content"));
				this.vo.setPhoto(sysFile);
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
		}
	}
	public void page()
	{
		String sql="select count(*) cnt from board3 where mid like ? or title like ?";
	
		try
		{
			PreparedStatement pst = conn.prepareStatement(sql);
			pst.setString(1, "%"+vo.getFind()+"%");
			pst.setString(2, "%"+vo.getFind()+"%");
			ResultSet rs = pst.executeQuery();
			if(rs.next())
			{
				totSize = rs.getInt("cnt");
				//전체 페이지수
				totPage = (int)Math.ceil(totSize*1.0/listSize);
				//전체 블럭수
				totBlock=(int)Math.ceil(totPage*1.0/blockSize);
				//현재 블럭
				nowBlock = (int)Math.ceil(nowPage*1.0/blockSize);
				//페이지의 시작과 끝 번호				
				endPage = nowBlock * blockSize;
				startPage = endPage-blockSize+1;
				//게시물의 시작번호 끝번호
				endNo = nowPage*listSize;
				startNo = endNo-listSize+1;
				//현재 블럭
				// endPage, endNo 보정값
				if(endPage>totPage) endPage=totPage;
				if(endNo > totSize) endNo=totSize;
			}
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}	
	}
	
	public ArrayList<BoardVO> select()
	{
		page();
		ArrayList<BoardVO> al = new ArrayList<BoardVO>();
		String sql = 
	            "select * from("
	            +"   select rownum rn, c.* from( "
	            +"   select * from board3 where mid like ? or title like ?)c"
	            +"   )b "
	            +"where b.rn between ? and ?";
		try
		{
			PreparedStatement pst =conn.prepareStatement(sql);
			pst.setString(1, "%"+vo.getFind()+"%");
			pst.setString(2, "%"+vo.getFind()+"%");
			pst.setInt(3, startNo);
			pst.setInt(4, endNo);
			
			ResultSet rs= pst.executeQuery();
			
			while(rs.next())
			{
				BoardVO v = new BoardVO();
				v.setPhoto(rs.getString("photo"));
				v.setTitle(rs.getString("title"));
				v.setUdate(rs.getString("udate"));
				al.add(v);
			}
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		
		return al;
	}
	public BoardVO getMvo() {
		return mvo;
	}

	public void setMvo(BoardVO vo) {
		this.vo = vo;
		this.nowPage=vo.getNowPage();
		this.nowBlock=vo.getNowBlock();

	
		
		
	}

	public Connection getConn() {
		return conn;
	}

	public void setConn(Connection conn) {
		this.conn = conn;
	}

	public int getListSize() {
		return listSize;
	}

	public void setListSize(int listSize) {
		this.listSize = listSize;
	}

	public int getBlockSize() {
		return blockSize;
	}

	public void setBlockSize(int blockSize) {
		this.blockSize = blockSize;
	}

	public int getTotSize() {
		return totSize;
	}

	public void setTotSize(int totSize) {
		this.totSize = totSize;
	}

	public int getTotPage() {
		return totPage;
	}

	public void setTotPage(int totPage) {
		this.totPage = totPage;
	}

	public int getTotBlock() {
		return totBlock;
	}

	public void setTotBlock(int totBlock) {
		this.totBlock = totBlock;
	}

	public int getNowPage() {
		return nowPage;
	}

	public void setNowPage(int nowPage) {
		this.nowPage = nowPage;
	}

	public int getNowBlock() {
		return nowBlock;
	}

	public void setNowBlock(int nowBlock) {
		this.nowBlock = nowBlock;
	}

	public int getStartNo() {
		return startNo;
	}

	public void setStartNo(int startNo) {
		this.startNo = startNo;
	}

	public int getEndNo() {
		return endNo;
	}

	public void setEndNo(int endNo) {
		this.endNo = endNo;
	}

	public int getStartPage() {
		return startPage;
	}

	public void setStartPage(int startPage) {
		this.startPage = startPage;
	}

	public int getEndPage() {
		return endPage;
	}

	public void setEndPage(int endPage) {
		this.endPage = endPage;
	}

}
